<template>
  <div class="execution">
    <basic-container>
      语言：
      <el-select class="code-mode-select" v-model="mode"
                 @change="changeMode">
        <el-option v-for="mode in modes"
                   :key="mode.value" :label="mode.label" :value="mode.value">
        </el-option>
      </el-select>
      &nbsp;&nbsp;样式：
      <el-select class="code-mode-select" v-model="theme"  @change="changeThemes">
        <option selected value="cobalt"></option>
        <el-option value="3024-day"></el-option>
        <el-option value="3024-night"></el-option>
        <el-option value="abcdef"></el-option>
        <el-option value="ambiance"></el-option>
        <el-option value="ayu-dark"></el-option>
        <el-option value="ayu-mirage"></el-option>
        <el-option value="base16-dark"></el-option>
        <el-option value="base16-light"></el-option>
        <el-option value="bespin"></el-option>
        <el-option value="blackboard"></el-option>
        <el-option value="colorforth"></el-option>
        <el-option value="darcula"></el-option>
        <el-option value="dracula"></el-option>
        <el-option value="duotone-dark"></el-option>
        <el-option value="duotone-light"></el-option>
        <el-option value="eclipse"></el-option>
        <el-option value="elegant"></el-option>
        <el-option value="erlang-dark"></el-option>
        <el-option value="gruvbox-dark"></el-option>
        <el-option value="hopscotch"></el-option>
        <el-option value="icecoder"></el-option>
        <el-option value="idea"></el-option>
        <el-option value="isotope"></el-option>
        <el-option value="lesser-dark"></el-option>
        <el-option value="liquibyte"></el-option>
        <el-option value="lucario"></el-option>
        <el-option value="material"></el-option>
        <el-option value="material-darker"></el-option>
        <el-option value="material-palenight"></el-option>
        <el-option value="material-ocean"></el-option>
        <el-option value="mbo"></el-option>
        <el-option value="mdn-like"></el-option>
        <el-option value="midnight"></el-option>
        <el-option value="monokai"></el-option>
        <el-option value="moxer"></el-option>
        <el-option value="neat"></el-option>
        <el-option value="neo"></el-option>
        <el-option value="night"></el-option>
        <el-option value="nord"></el-option>
        <el-option value="oceanic-next"></el-option>
        <el-option value="panda-syntax"></el-option>
        <el-option value="paraiso-dark"></el-option>
        <el-option value="paraiso-light"></el-option>
        <el-option value="pastel-on-dark"></el-option>
        <el-option value="railscasts"></el-option>
        <el-option value="rubyblue"></el-option>
        <el-option value="seti"></el-option>
        <el-option value="shadowfox"></el-option>
        <el-option value="solarized dark"></el-option>
        <el-option value="solarized light"></el-option>
        <el-option value="the-matrix"></el-option>
        <el-option value="tomorrow-night-bright"></el-option>
        <el-option value="tomorrow-night-eighties"></el-option>
        <el-option value="ttcn"></el-option>
        <el-option value="twilight"></el-option>
        <el-option value="vibrant-ink"></el-option>
        <el-option value="xq-dark"></el-option>
        <el-option value="xq-light"></el-option>
        <el-option value="yeti"></el-option>
        <el-option value="yonce"></el-option>
        <el-option value="zenburn"></el-option>
      </el-select>
      <p></p>
      <textarea ref="textarea"></textarea>
      <p></p>
      <el-footer align="center">
        <el-button type="primary" @click="saveUpdate" :loading="fullscreenLoading">保存修改</el-button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button type="info" @click="closeParentDialogWin">取消</el-button>
      </el-footer>
    </basic-container>
  </div>
</template>

<script type="text/ecmascript-6">
  import  { updateCodeTemplate } from '@/api/code-template'
  // 引入全局实例
  import _CodeMirror from 'codemirror'

  // 核心样式
  import 'codemirror/lib/codemirror.css'
  // 引入主题后还需要在 options 中指定主题才会生效
  import 'codemirror/theme/cobalt.css'
  import 'codemirror/theme/3024-day.css'
  import 'codemirror/theme/3024-night.css'
  import 'codemirror/theme/abcdef.css'
  import 'codemirror/theme/ambiance.css'
  import 'codemirror/theme/ayu-dark.css'
  import 'codemirror/theme/ayu-mirage.css'
  import 'codemirror/theme/base16-dark.css'
  import 'codemirror/theme/bespin.css'
  import 'codemirror/theme/base16-light.css'
  import 'codemirror/theme/blackboard.css'
  import 'codemirror/theme/colorforth.css'
  import 'codemirror/theme/dracula.css'
  import 'codemirror/theme/duotone-dark.css'
  import 'codemirror/theme/duotone-light.css'
  import 'codemirror/theme/eclipse.css'
  import 'codemirror/theme/elegant.css'
  import 'codemirror/theme/erlang-dark.css'
  import 'codemirror/theme/gruvbox-dark.css'
  import 'codemirror/theme/hopscotch.css'
  import 'codemirror/theme/icecoder.css'
  import 'codemirror/theme/isotope.css'
  import 'codemirror/theme/lesser-dark.css'
  import 'codemirror/theme/liquibyte.css'
  import 'codemirror/theme/lucario.css'
  import 'codemirror/theme/material.css'
  import 'codemirror/theme/material-darker.css'
  import 'codemirror/theme/material-palenight.css'
  import 'codemirror/theme/material-ocean.css'
  import 'codemirror/theme/mbo.css'
  import 'codemirror/theme/mdn-like.css'
  import 'codemirror/theme/midnight.css'
  import 'codemirror/theme/monokai.css'
  import 'codemirror/theme/moxer.css'
  import 'codemirror/theme/neat.css'
  import 'codemirror/theme/neo.css'
  import 'codemirror/theme/night.css'
  import 'codemirror/theme/nord.css'
  import 'codemirror/theme/oceanic-next.css'
  import 'codemirror/theme/panda-syntax.css'
  import 'codemirror/theme/paraiso-dark.css'
  import 'codemirror/theme/paraiso-light.css'
  import 'codemirror/theme/pastel-on-dark.css'
  import 'codemirror/theme/railscasts.css'
  import 'codemirror/theme/rubyblue.css'
  import 'codemirror/theme/seti.css'
  import 'codemirror/theme/shadowfox.css'
  import 'codemirror/theme/solarized.css'
  import 'codemirror/theme/the-matrix.css'
  import 'codemirror/theme/tomorrow-night-bright.css'
  import 'codemirror/theme/tomorrow-night-eighties.css'
  import 'codemirror/theme/ttcn.css'
  import 'codemirror/theme/twilight.css'
  import 'codemirror/theme/vibrant-ink.css'
  import 'codemirror/theme/xq-dark.css'
  import 'codemirror/theme/xq-light.css'
  import 'codemirror/theme/yeti.css'
  import 'codemirror/theme/idea.css'
  import 'codemirror/theme/darcula.css'
  import 'codemirror/theme/yonce.css'
  import 'codemirror/theme/zenburn.css'

  // 需要引入具体的语法高亮库才会有对应的语法高亮效果
  // codemirror 官方其实支持通过 /addon/mode/loadmode.js 和 /mode/meta.js 来实现动态加载对应语法高亮库
  // 但 vue 貌似没有无法在实例初始化后再动态加载对应 JS ，所以此处才把对应的 JS 提前引入
  import 'codemirror/mode/javascript/javascript.js'
  import 'codemirror/mode/css/css.js'
  import 'codemirror/mode/xml/xml.js'
  import 'codemirror/mode/clike/clike.js'
  import 'codemirror/mode/markdown/markdown.js'
  import 'codemirror/mode/python/python.js'
  import 'codemirror/mode/r/r.js'
  import 'codemirror/mode/shell/shell.js'
  import 'codemirror/mode/sql/sql.js'
  import 'codemirror/mode/swift/swift.js'
  import 'codemirror/mode/vue/vue.js'
  import 'codemirror/mode/velocity/velocity.js'

  // 尝试获取全局实例
  const CodeMirror = window.CodeMirror || _CodeMirror

  export default {
    name: 'in-coder',
    props: {
      selectRecord: null,
      // 外部传入的内容，用于实现双向绑定
      value: String,
      // 外部传入的语法类型
      language: {
        type: String,
        default: null
      }
    },
    watch: {
      selectRecord: {
        deep: true,  // 深度监听
        handler(newVal,oldVal) {
          this.codeTemplateId = newVal.id
          this.codeValue = this.selectRecord.content;
          this.coder.setValue(this.codeValue)
        }
      }
    },
    data () {
      return {
        codeTemplateId:null,
        fullscreenLoading: false,
        // 内部真实的内容
        codeValue: '',
        // 默认的语法类型
        mode: 'javascript',
        // 编辑器实例
        coder: null,
        theme:'cobalt',
        // 默认配置
        options: {
          // 缩进格式
          tabSize: 2,
          // 主题，对应主题库 JS 需要提前引入
          theme: 'cobalt',
          // 显示行号
          lineNumbers: true,
          line: true
        },
        // 支持切换的语法高亮类型，对应 JS 已经提前引入
        // 使用的是 MIME-TYPE ，不过作为前缀的 text/ 在后面指定时写死了
        modes: [{
          value: 'css',
          label: 'CSS'
        }, {
          value: 'javascript',
          label: 'Javascript'
        }, {
          value: 'html',
          label: 'XML/HTML'
        }, {
          value: 'x-java',
          label: 'Java'
        }, {
          value: 'x-objectivec',
          label: 'Objective-C'
        }, {
          value: 'x-python',
          label: 'Python'
        }, {
          value: 'x-rsrc',
          label: 'R'
        }, {
          value: 'x-sh',
          label: 'Shell'
        }, {
          value: 'x-sql',
          label: 'SQL'
        }, {
          value: 'x-swift',
          label: 'Swift'
        }, {
          value: 'x-vue',
          label: 'Vue'
        },{
          value: 'text/velocity',
          label: 'Velocity'
        }, {
          value: 'markdown',
          label: 'Markdown'
        }],
        themes: [{
          value: 'css',
          label: 'CSS'
        }, {
          value: 'javascript',
          label: 'Javascript'
        }, {
          value: 'html',
          label: 'XML/HTML'
        }, {
          value: 'x-java',
          label: 'Java'
        }, {
          value: 'x-objectivec',
          label: 'Objective-C'
        }, {
          value: 'x-python',
          label: 'Python'
        }, {
          value: 'x-rsrc',
          label: 'R'
        }, {
          value: 'x-sh',
          label: 'Shell'
        }, {
          value: 'x-sql',
          label: 'SQL'
        }, {
          value: 'x-swift',
          label: 'Swift'
        }, {
          value: 'x-vue',
          label: 'Vue'
        },{
          value: 'text/velocity',
          label: 'Velocity'
        }, {
          value: 'markdown',
          label: 'Markdown'
        }]
      }
    },
    mounted () {
      // 初始化
      this.codeTemplateId = this.selectRecord.id
      this.codeValue = this.selectRecord.content;
      this._initialize()
    },
    methods: {
      closeParentDialogWin(){
        this.$emit('par', 'closeCodeMaitainDialog','false');
      },
      saveUpdate(){
        let _this = this
        this.fullscreenLoading = true
        updateCodeTemplate(this.codeTemplateId,this.codeValue).then(data => {
          this.$message({
            showClose: true,
            message: data.data ? '修改成功':"修改失败",
            type:data.data ? 'success':'error'
          })
          if( data.data ){
            this.$emit('par', 'closeCodeMaitainDialog',true);
          }
          this.fullscreenLoading = false
        }).catch(function(err) {
          _this.fullscreenLoading = false
        })
      },
      // 初始化
      _initialize () {
        // 初始化编辑器实例，传入需要被实例化的文本域对象和默认配置
        this.coder = CodeMirror.fromTextArea(this.$refs.textarea, this.options)
        // 编辑器赋值
        this.coder.setValue(this.codeValue)
        //this.coder.setValue(this.value || this.code)

        // 支持双向绑定
        this.coder.on('change', (coder) => {
          this.codeValue = coder.getValue()
        })

        this.coder.setSize('auto','500px')


        // 尝试从父容器获取语法类型
        if (this.language) {
          // 获取具体的语法类型对象
          let modeObj = this._getLanguage(this.language)

          // 判断父容器传入的语法是否被支持
          if (modeObj) {
            this.mode = modeObj.label
          }
        }
      },
      // 获取当前语法类型
      _getLanguage (language) {
        // 在支持的语法类型列表中寻找传入的语法类型
        return this.modes.find((mode) => {
          // 所有的值都忽略大小写，方便比较
          let currentLanguage = language.toLowerCase()
          let currentLabel = mode.label.toLowerCase()
          let currentValue = mode.value.toLowerCase()

          // 由于真实值可能不规范，例如 java 的真实值是 x-java ，所以讲 value 和 label 同时和传入语法进行比较
          return currentLabel === currentLanguage || currentValue === currentLanguage
        })
      },
      // 更改模式
      changeMode (val) {
        // 修改编辑器的语法配置
        this.coder.setOption('mode', `text/${val}`)

        // 获取修改后的语法
        let label = this._getLanguage(val).label.toLowerCase()

        // 允许父容器通过以下函数监听当前的语法值
        this.$emit('language-change', label)
      },
      changeThemes(val){
        this.coder.setOption("theme",val)
      }
    }
  }
</script>

<style scoped>
  .CodeMirror-scroll {
    overflow: scroll !important;
    margin-bottom: 0;
    margin-right: 0;
    padding-bottom: 0;
    height: auto;
    outline: none;
    position: relative;
    border: 1px solid #dddddd;
    padding: 20px 10px;
  }
</style>
